package com.jbm.jbmsupplier.activity.register.view

import android.support.design.widget.TextInputLayout
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.TextView
import com.jbm.jbmsupplier.R
import com.jbm.jbmsupplier.activity.AllActivityApp
import com.jbm.jbmsupplier.activity.register.presenter.Register_Present
import com.jbm.jbmsupplier.utlis.ExitActivity
import kotlinx.android.synthetic.main.lz_titlehurdle.*
import kotlinx.android.synthetic.main.register_manage.*

class Register_Manages : AllActivityApp(), View.OnClickListener, Register_Manage_Massages {
    override fun getPasWordzc(): String {
        return tiet_passwordzc.editableText.toString()
    }

    override fun getPasWordErrzc(): TextInputLayout {
        return til_passwordzc
    }

    override fun getTextView(): TextView {
        return register_butt_hqyzm
    }

    /**
     * 此接口共用显示错误信息
     */
    override fun getPhoneErr(): TextInputLayout {
        return til_photo
    }

    override fun getValidateCodeErr(): TextInputLayout {
        return til_yzm
    }

    override fun getPasWordErr(): TextInputLayout {
        return til_password
    }

    override fun getUnitNameErr(): TextInputLayout {
        return til_srdw
    }

    var register_present: Register_Present? = null

    override fun setContentView(): Int {//加载布局
        return R.layout.register_user
    }

    override fun initIalizeView() {//初始化数据
        ExitActivity.getInstance().addActivity(this)
        register_butt_hqyzm.setOnClickListener(this)
        register_manage_button.setOnClickListener(this)
        teleft.setOnClickListener(this)
        register_present = Register_Present(this,this);
        tetitle.text="注册管理员"
        tiet_photo.addTextChangedListener( MyWatcher(tiet_photo))
        tiet_yzm.addTextChangedListener( MyWatcher(tiet_yzm))
        tiet_password.addTextChangedListener( MyWatcher(tiet_password))
        tiet_srdw.addTextChangedListener( MyWatcher(tiet_srdw))
    }

    override fun getPhone(): String {//获取手机号
            return tiet_photo.editableText.toString()

}

    override fun getValidateCode(): String {//获取验证码
            return tiet_yzm.editableText.toString()


    }

    override fun getPasWord(): String {//获取密码
            return tiet_password.editableText.toString()


    }

    override fun getUnitName(): String {//获取单位名字
            return tiet_srdw.editableText.toString()


    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.register_manage_button -> {//注册管理员
                register_present?.getRegisterUserMassage(this)
            }
            R.id.register_butt_hqyzm -> {//获取验证码
                register_present?.getValidateCode()
            }
            R.id.teleft -> {
                finish()

            }

        }
    }

    inner  internal class MyWatcher : TextWatcher {
        var view:View?=null
        constructor(view: View?) {
            this.view = view
        }
        override fun afterTextChanged(s: Editable?) {
            when(view?.id){
                R.id.tiet_photo ->{
                    if (s!!.length>11){
                        til_photo.isErrorEnabled=false
                    }else if (!s.toString().equals(""))
                        til_photo.isErrorEnabled=false
                }
                R.id.tiet_yzm ->{
                    if (!s.toString().equals(""))
                        til_yzm.isErrorEnabled=false
                }
                R.id.tiet_password ->{
                    if (!s.toString().equals(""))
                        til_password.isErrorEnabled=false
                }
                R.id.tiet_passwordzc ->{
                    if (!s.toString().equals(""))
                        til_passwordzc.isErrorEnabled=false
                }
                R.id.tiet_srdw ->{
                    if (!s.toString().equals(""))
                        til_srdw.isErrorEnabled=false
                }


            }

        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

    }
}
